package com.bwie.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Contract;
import com.bwie.common.domain.GlobalStatusEnum;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.ReviewRequest;
import com.bwie.common.domain.request.ShopContractAddRequest;
import com.bwie.common.domain.response.ShopContractListResponse;
import com.bwie.common.utils.JwtUtils;
import com.bwie.shop.exception.ShopException;
import com.bwie.shop.mapper.ShopContractMapper;
import com.bwie.shop.mapper.ShopMapper;
import com.bwie.shop.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author <a href="mailto:3364114353@qq.com">liuwenliang</a>
 * @version 1.0
 * @description:
 * @date 2025/9/9 14:56
 */
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ShopContractMapper shopContractMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public List<ShopContractListResponse> getContractList() {
        List<ShopContractListResponse> contractListResponses = shopContractMapper.getContractList();
        return contractListResponses;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void pass(ReviewRequest reviewRequest) {
        //获取接收数据
        String status = reviewRequest.getStatus();
        if (status == null) {
            throw new ShopException(GlobalStatusEnum.SHOP_ID_IS_NULL.getLabel());
        }
        //2 为已续约  3为待续约
        if (status.equals("1")) {
            //根据编号更改状态为通过
            shopContractMapper.updateStatusIsPassById(reviewRequest.getId());
            //更改成功后更改店铺状态为已续约
            shopMapper.updateStatusByShopId(reviewRequest.getShopId());
        }
    }

    @Override
    public void notPassed(ReviewRequest reviewRequest) {
        //获取接收数据
        String status = reviewRequest.getStatus();
        if (status == null) {
            throw new ShopException(GlobalStatusEnum.SHOP_ID_IS_NULL.getLabel());
        }
        //2 为已续约  3为待续约
        if (status.equals("1")) {
            //根据编号更改状态为通过
            shopContractMapper.updateStatusIsNotPassedById(reviewRequest.getId());
        }
    }

    @Override
    public void add(ShopContractAddRequest shopContractAddRequest) {
        //参数校验
        parameterVerification(shopContractAddRequest);
        //数据转换
        Contract contract = new Contract();
        BeanUtil.copyProperties(shopContractAddRequest,contract);
        //获取当前登录人信息
        User loginUserInfo = getLoginUserInfo();
        contract.setUserId(loginUserInfo.getId());
        contract.setStatus("1");
        //添加
        shopContractMapper.add(contract);
    }



    private void parameterVerification(ShopContractAddRequest shopContractAddRequest) {
        //获取接收数据
        Integer shopId = shopContractAddRequest.getShopId();
        Date startTime = shopContractAddRequest.getStartTime();
        Date endTime = shopContractAddRequest.getEndTime();
        //非空校验
        if (ObjUtil.isNull(startTime)) {
            throw new ShopException(GlobalStatusEnum.START_TIME_IS_NULL.getLabel());
        }
        if (ObjUtil.isNull(endTime)) {
            throw new ShopException(GlobalStatusEnum.END_TIME_IS_NULL.getLabel());
        }
        if (ObjUtil.isNull(shopId)) {
            throw new ShopException(GlobalStatusEnum.SHOP_NAME_IS_NULL.getLabel());
        }
    }

    public User getLoginUserInfo() {
        //获取请求头
        String token = request.getHeader(TokenConstants.TOKEN);
        //根据token解析userKey
        String userKey = JwtUtils.getUserKey(token);
        //从缓存中获取当前登录人信息
        User loginUser = (User) redisTemplate.opsForValue().get(userKey);
        return loginUser;
    }
}
